package kaist.sep521.lego.circlebicycle;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class GroupArrayAdapter extends ArrayAdapter<String> {
	private Context mContext;
	private TextView groupName;
	private Button joinButton;
	private ArrayList<String> groupList = new ArrayList<String>();

	public GroupArrayAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
		super(context, textViewResourceId, objects);
		this.mContext = context;
		this.groupList = objects;
	}

	public int getCount() {
		return this.groupList.size();
	}

	public String getItem(int index) {
		return this.groupList.get(index);
	}

	private CircleActivity mAct;
	public void setActivity(CircleActivity act) {
		mAct = act;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		if (row == null) {
			// ROW INFLATION
			LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.group_listitem, parent, false);
		}

		// Get item
		final String nGroup = getItem(position);
		groupName = (TextView) row.findViewById(R.id.listitem_group_name);
		groupName.setText(nGroup);
		groupName.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// move to make new group window
				Intent intent = new Intent(getContext(), CircleInfoActivity.class);
				intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
				intent.putExtra("name", nGroup);
				getContext().startActivity(intent);
			}
		});

		joinButton = (Button)row.findViewById(R.id.listitem_join_button);
		joinButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// request to join group
				circleJoin(nGroup);
				//				MainActivity.myInfo.setGroup(nGroup);
				mAct.update();
			}
		});

		return row;
	}

	private void circleJoin(String groupname) {
		String user = MainActivity.myInfo.getID();
		String joinedGroup = MainActivity.myInfo.getGroup();

		String cexitUrl = "http://14.63.196.253/~lego/groupjoin.php?user=" + user + "&group=" + groupname;

		if (groupname.equals(joinedGroup)){
			Toast.makeText(mContext, "You are already joined this "+ groupname + " Circle.", Toast.LENGTH_SHORT).show();
		}else{
			//Clear the Voice Tag on the server.
			try {
				URL url = new URL(cexitUrl);
				HttpURLConnection conn = (HttpURLConnection) url.openConnection();

				if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
					MainActivity.myInfo.setGroup(groupname);
					Log.w("Circle JOIN", "HTTP_SEND_OK");

				} else {
					Toast.makeText(mContext, "Server connection error!!", Toast.LENGTH_SHORT).show();
				}
				conn.disconnect();

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}